package de.fhkiel.webservice.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import de.fhkiel.webservice.data.Item;

@Repository
public class ItemDAOImpl implements ItemDAO {

	@Autowired
    private SessionFactory sessionFactory;
	
	@Override
	public void addItem(Item item) {
		sessionFactory.getCurrentSession().save(item);
	}

	@Override
	public List<Item> listItem() {
		return sessionFactory.getCurrentSession().createQuery("from Item").list();
	}

	@Override
	public void removeItem(Integer id) {
		Item item = (Item) sessionFactory.getCurrentSession().load(Item.class, id);
        if (item != null) {
            sessionFactory.getCurrentSession().delete(item);
        }
	}

}
